using System;
using DevExpress.Xpo.DB;
using System.ServiceModel;
using DevExpress.Xpo;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                CreateDataStore();
            }
            else
            {
                CreateCachedDataStore();
            }
        }
        private static void CreateDataStore()
        {
            // Create our real data store
            IDataStore sourceDataStore = XpoDefault.GetConnectionProvider(AutoCreateOption.DatabaseAndSchema);

            //DataCacheRoot
            // Create the wrapper class
            DataStoreServerProxy publicationObject = new DataStoreServerProxy(sourceDataStore);

            // Publish the wrapper class via WCF. This could obviously be done with config files instead of code,
            // using different bindings and endpoints, whatever...

            //Uri baseAddress = new Uri("net.tcp://0.0.0.0:7465/");
            //ServiceHost serviceHost = new ServiceHost(publicationObject, baseAddress);
            using (ServiceHost serviceHost = new ServiceHost(publicationObject))
            {
                //Binding binding = new NetTcpBinding(SecurityMode.None);
                //serviceHost.AddServiceEndpoint(typeof(Common.IDataStoreContract), binding, "XPOService");
                serviceHost.Open();

                Console.WriteLine("Service published, hit Return to terminate.");
                Console.ReadLine();

                if (serviceHost.State != CommunicationState.Closed)
                {
                    serviceHost.Close();
                }
            }
        }
        private static void CreateCachedDataStore()
        {
            // Create our real data store
            IDataStore sourceDataStore = XpoDefault.GetConnectionProvider(AutoCreateOption.DatabaseAndSchema);

            DataCacheRoot cacheRoot = new DataCacheRoot(sourceDataStore);

            //DataCacheRoot
            // Create the wrapper class
            CachedDataStoreServerProxy publicationObject = new CachedDataStoreServerProxy(cacheRoot);

            // Publish the wrapper class via WCF. This could obviously be done with config files instead of code,
            // using different bindings and endpoints, whatever...

            //Uri baseAddress = new Uri("net.tcp://0.0.0.0:7465/");
            //ServiceHost serviceHost = new ServiceHost(publicationObject, baseAddress);
            using (ServiceHost serviceHost = new ServiceHost(publicationObject))
            {
                //Binding binding = new NetTcpBinding(SecurityMode.None);
                //serviceHost.AddServiceEndpoint(typeof(Common.IDataStoreContract), binding, "XPOService");
                serviceHost.Open();

                Console.WriteLine("Service published, hit Return to terminate.");
                Console.ReadLine();

                if (serviceHost.State != CommunicationState.Closed)
                {
                    serviceHost.Close();
                }
            }
        }
    }
}
